package org.linlinjava.litemall.db.service;


import org.linlinjava.litemall.db.dao.LitemallUserMemberMapper;
import org.linlinjava.litemall.db.domain.LitemallUserMember;
import org.linlinjava.litemall.db.domain.LitemallUserMemberExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Administrator
 */
@Service
public class LitemallUserMemberService {
    @Resource
    private LitemallUserMemberMapper userMemberMapper;


    public LitemallUserMember findById(Integer id) {
        LitemallUserMemberExample example = new LitemallUserMemberExample();
        example.or().andIdEqualTo(id).andDeletedEqualTo(false);
        return userMemberMapper.selectOneByExample(example);
    }

    public LitemallUserMember findByUid(Integer userId) {
        LitemallUserMemberExample userMemberExample = new LitemallUserMemberExample();
        userMemberExample.or().andUserIdEqualTo(userId).andEffectiveEqualTo(true).andDeletedEqualTo(false);
        userMemberExample.orderBy("add_time desc");
        return userMemberMapper.selectOneByExample(userMemberExample);
    }

    public List<LitemallUserMember> findAllByUserId(Integer userId){
        LitemallUserMemberExample userMemberExample = new LitemallUserMemberExample();
        userMemberExample.or().andUserIdEqualTo(userId);
        return userMemberMapper.selectByExample(userMemberExample);
    }


    public void updateById(LitemallUserMember userMember) {
        userMember.setUpdateTime(LocalDateTime.now());
        userMemberMapper.updateByPrimaryKeySelective(userMember);
    }

    public List<LitemallUserMember> findActiveList() {
        LitemallUserMemberExample example = new LitemallUserMemberExample();
        example.or().andEffectiveEqualTo(true).andDeletedEqualTo(false);

        return userMemberMapper.selectByExample(example);
    }

}
